<?php
namespace app\admin\controller;

use think\Controller;
use think\Session;
use think\Db;
use think\Request;

class Open extends Controller
{
    /**
     * 系统登录
     */
    public function Login(){
        //Post请求操作
        if(IsPost()){
            $Data = Param();

            $AdminInfo = Db::name('admin')->where(['username'=>$Data['username']])->find();
//            dump(md5($Data['password']));
//
//            dump($AdminInfo);
//            die;
            if(empty($AdminInfo)){
                return ['status'=>0,'message'=>'用户名或密码错误'];
            }

            //判断验证码是否正确
            $CodeRes = checkCode($AdminInfo['phone'],$Data['code']);
            // var_dump($CodeRes);die;
            if($CodeRes['status'] != 1){
                return $CodeRes;
            }

            if(md5($Data['password']) !== $AdminInfo['password']){
                return ['status'=>0,'message'=>'用户名或密码错误'];
            }
            
            $ip = Request::instance()->ip();
            $logininfo['ip'] = $ip;
            $logininfo['user_id'] = $AdminInfo['id'];
            $logininfo['time'] = date('Y-m-d H:i:s');
            $logininfo['type'] = 1;//后台访问
            if(Db::name('user_login')->insert($logininfo)){
                Session::set('admin_id',$AdminInfo['id']);
                return ['status'=>1,'message'=>'登陆成功'];
            }else{
                return ['status'=>0,'message'=>'系统问题，请稍候再试'];
            }

            
        }
        //判断用户是否已经登录
        $AdminID = Session::get('admin_id');
        //系统参数
        // $Config = Config();
        
        // $phone = $Config['phone'];
        // $this->assign('phone',$phone);
        if(empty($AdminID)){
            return $this->fetch('Login');
        }else{
            $this->redirect('Index/index');
        }
    }

    /**
     * 退出登录
     */
    public function LoginOut(){
        //注销Session信息
        Session::delete('admin_id');

        //跳转登录界面
        $this->redirect('Open/login');
    }
    
    /**
     * 发送短信验证码
     * @return array
     */
    public function SendCode(){
        //引用发送短信类
        require 'Smsapi.php';
        //接口账号
        $uid = 'yqdyqd111';
        //登录密码
        $pwd = 'yqdyqd111';

        /**
         * 实例化接口
         *
         * @param string $uid 接口账号
         * @param string $pwd 接口密码
         */
        $api = new SmsApi($uid,$pwd);

        //发送的手机 多个号码用,英文逗号隔开
        $username = param()['username'];
        $info = Db::name('admin')->where(['username'=>$username])->find();
        if(!$info){
            return ['status'=>0,'message'=>"您不是管理员,无权访问"];
        }
        
        
        $mobile = $info['phone'];
        //验证码
        $code = $api->randNumber();
        //短信内容
        $content = '【芭菲名媛】您的验证码是：'.$code.'，请及时完成验证。';

        //发送全文模板短信
        $result = $api->sendAll($mobile,$content);

        if($result['stat']=='100'){
            AddCode($mobile,$code);
            return ['status'=>1,'message'=>"发送成功"];
        }else{
            return ['status'=>0,'message'=>'发送失败:'.$result['stat'].'('.$result['message'].')'];
        }
    }
    
    public function aa(){
        echo(111);
    }
}